<?php

namespace common\models;

use Yii;
use common\helpers\Util;

/**
 * This is the model class for table "EmploymentPause".
 */
class EmploymentPause extends ActiveRecord {

    // @name 日志操作人类型定义
    const StatusInit = 0;
    const StatusConfirm = 1;
    const StatusSuccess = 10;
    const StatusRefuse = 11;
    public static $handlerSelector = [
        self::StatusInit => ['title' => '申请暂停', 'status' => 'gray'],
        self::StatusConfirm => ['title' => '接受暂停', 'status' => 'blue'],
        self::StatusSuccess => ['title' => '已完成', 'status' => 'green'],
        self::StatusRefuse => ['title' => '拒绝暂停', 'status' => 'red'],
    ];

    // only define rules for those attributes that
    // will receive user inputs.
    public function rules()
    {
        return [
            [['employment_id', 'start_day', 'days'], 'required'],
            [['employment_id', 'start_day', 'days', 'status'], 'integer'],
            [['remark'], 'string', 'max' => 255],
        ];
    }
    /**
     * @name 字段名称
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'employment_id' => '雇佣编号',
            'start_day' => '开始日期',
            'days' => '暂停天数',
            'status' => '状态',
            'remark' => '备注',
        ];
    }
    /**
     * @name update & insert data check config for html
     * @param $type string 操作类型
     * @param $encodeJson boolean 是否转成JSON字符串
     * @return array
     */
    public static function flyer($type = 'insert')
    {
        return [
            'param' => [
                'employment_id' => ['雇佣编号', ['int', 'required']],
                'start_day' => ['开始日期', ['date' => 'Y-m-d', 'required']],
                'days' => ['暂停天数', ['int', 'gt' => 0, 'required']],
                'remark' => ['备注', ['maxlength' => 255]],
            ],
        ];
    }

    /**
     * 暂停成功
     * @return boolean
     */
    public function success()
    {
        if( ! in_array($this->status, [static::StatusInit, static::StatusConfirm])) {
            return false;
        }
        $this->status = static::StatusSuccess;
        $this->updated_at = time();
        return $this->save();
    }
    
    /**
     * 创建暂停订单
     * @return mixed
     */
    public static function creator($params)
    {
        if(empty($params)) {
            return false;
        }
        $pause = new static();
        $pause->loads($params);
        $pause->status = static::StatusInit;
        if( ! ($pause->validate() && $pause->save())) {
            return false;
        }
        return $pause;
    }
}